#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time
import math

DO = 22
Buzz = 18
GPIO.setmode(GPIO.BCM)

def setup():
    ADC.setup(0x48)
    GPIO.setup  (DO,    GPIO.IN)

def Print(x):
    if x == 1:
        print ('')
        print ('   *********')
        print ('   * Safe~ *')
    if x == 0:
        print ('   * Danger Gas! *')

def loop():
    status = 1
    count = 0
    while True:
        print ('ADC.read(0)==' , ADC.read(0))  #有烟雾时，该值增大
        
        tmp = GPIO.input(DO);
        print ('tmp==' ,tmp)    
#无烟雾时为高电平，tmp=1,打印safe，有烟雾时为低电平，打印Danger Gas！
        if tmp != status:
            Print(tmp)
            status = tmp
        else:
            count = 0

        time.sleep(0.2)

def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt: 
        destroy()


